﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WallpaperDemo.Models;

namespace WallpaperDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WallpapersController : ControllerBase
    {
        private readonly WallpapersContext _context;
        private WallpaperService wallpaperService;
        public WallpapersController(WallpapersContext context)
        {
            _context = context;
            wallpaperService = new WallpaperService(context);
        }

        // GET: api/Wallpapers
        [HttpGet]
        public ActionResult<IEnumerable<Wallpaper>> GetWallpapers()
        {
          if (_context.Wallpapers == null)
          {
              return NotFound();
          }
            return wallpaperService.Wallpapers;
        }

        // GET: api/Wallpapers/5
        [HttpGet("{id}")]
        public ActionResult<Wallpaper> GetWallpaper(string id)
        {
          if (_context.Wallpapers == null)
          {
              return NotFound();
          }
            var wallpaper = wallpaperService.QueryWallpaper_Id(id);

            if (wallpaper == null)
            {
                return NotFound();
            }

            return wallpaper;
        }

        // GET: api/Wallpapers/updater=admin
        [HttpGet("updater={username}")]
        public ActionResult<List<Wallpaper>> GetWallpaper_User(string username)
        {
            if (_context.Wallpapers == null)
            {
                return NotFound();
            }
            var wallpaper = wallpaperService.QueryWallpaper_User(username);

            if (wallpaper == null)
            {
                return NotFound();
            }

            return wallpaper;
        }

        // GET: api/Wallpapers/cat/nature/seq=1
        [HttpGet("cat/{cat}/seq={seq}")]
        public ActionResult<List<Wallpaper>> GetWallpaper_Cat(string cat, int seq)
        {
            var wallpaper = wallpaperService.QueryWallpaper_Cat(cat, seq);
            if (wallpaper == null)
            {
                return NotFound();
            }

            return wallpaper;
        }

        // PUT: api/Wallpapers/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public ActionResult PutWallpaper(string id, Wallpaper wallpaper)
        {
            if (id != wallpaper.WallpaperId)
            {
                return BadRequest();
            }

            try
            {
                wallpaperService.RemoveWallpaper(id);
                wallpaperService.AddWallpaper(wallpaper);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WallpaperExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Wallpapers
        [HttpPost]
        public ActionResult<Wallpaper> AddWallpaper(Wallpaper wallpaper)
        {
            try
            {
                wallpaperService.AddWallpaper(wallpaper);
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }

            return wallpaper;
        }

        // DELETE: api/Wallpapers/5
        [HttpDelete("{id}")]
        public ActionResult<Wallpaper> DeleteWallpaper(string id)
        {
            try
            {
                wallpaperService.RemoveWallpaper(id);
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }
            return NoContent();
        }

        private bool WallpaperExists(string id)
        {
            return (_context.Wallpapers?.Any(e => e.WallpaperId == id)).GetValueOrDefault();
        }
    }
}
